/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-3-19 21:26:20
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#ifndef INCLUDE_RPC_CLIENT_MGR_H_
#define INCLUDE_RPC_CLIENT_MGR_H_

#include "base/xrefptr.h"
#include "base/xmutex.h"
#include "base/xguard.h"
#include "base/xthread.h"
#include "net/net_reactor.h"
#include "protocol/net_binary_decoder.h"
#include "protocol/net_proto_def.h"
#include <map>

class CRpcClientBase;
class CRpcClientMgr : public IEvent_handler, public IBinaryDecoderListener
{
    friend class CRpcClientBase;
    CRpcClientMgr( const CRpcClientMgr& );
    const CRpcClientMgr& operator= ( const CRpcClientMgr& );
public:
    CRpcClientMgr();
    CRpcClientMgr(int threadnum);
    virtual ~CRpcClientMgr();
    
public:
    void free();
protected:
    smart::ref_ptr<CRpcClientBase> get_rpc_client( SOCK_FD fd );
    int add_rpc_client( CRpcClientBase* client );
    int del_rpc_client( SOCK_FD fd );
    void del_all_rpc_client();
private:
    virtual int on_connect( SOCK_FD listen_fd, SOCK_FD cli_fd, const char* remote_ip, int remote_port );
	virtual int on_data( SOCK_FD fd, const char* data, int len, const char* remote_ip, int remote_port );
	virtual int on_send_ack( SOCK_FD fd, int len );
	virtual int on_close( SOCK_FD fd, int error );
    virtual int onPDU( int fd, smart::ref_ptr<NetBinaryPdu> pdu );
	virtual int onError( int fd, BinaryDecoderError_t err );
private:
    net_reactor_t m_netReactor;
    std::map< SOCK_FD, smart::ref_ptr<CRpcClientBase> > m_rpc_clients_map;
    smart::mutex_t m_lock;
};


#endif //~INCLUDE_RPC_CLIENT_MGR_H_
